package cn.zhf.shop.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.zhf.shop.config.AccessTokenService;
import cn.zhf.shop.entity.ClassUser;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.SaoYiSao;
import cn.zhf.shop.entity.search.ClassUserSearch;
import cn.zhf.shop.service.ClassUserService;
import cn.zhf.shop.utils.Sign;
import cn.zhf.shop.utils.WxUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static cn.zhf.shop.utils.WxUtils.sendGet;

@RestController
@Api(tags = "用户模块")
@RequestMapping("/api/class_user/")
public class ClassUserController {

    @Resource
    private ClassUserService classUserService;

    @Resource
    private AccessTokenService accessTokenService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public Message<?> register(ClassUser c){
        return classUserService.register(c);
    }

    @ApiOperation(value = "小程序授权登录，如已注册过，返回用户信息，否则返回OpenId")
    @PostMapping("/login/jscode")
    public Message<?> miniProgramLogin(String jsCode){
        return classUserService.loginByMiniProgram(jsCode);
    }

    @ApiOperation(value = "短信验证码登录")
    @PostMapping("/login/captcha")
    public Message<?> captchaLogin(String userId){
        return Message.success("此处为预留接口");
    }

    @ApiOperation(value = "获取所有用户")
    @GetMapping("/all")
    public Message<List<ClassUser>> getAllUser(){
        return classUserService.getAll();
    }

    @ApiOperation(value = "根据机构id获取所有用户")
    @GetMapping("/org")
    public Message<List<ClassUser>> getUserByOrgId(int orgId){
        return classUserService.getUserByOrgId(orgId);
    }


    @ApiOperation(value = "根据编号获取用户信息")
    @GetMapping("/userId")
    public Message<ClassUser> getByOrgId(String userId){
        return classUserService.getUserInfoByUserId(userId);
    }

    @ApiOperation(value = "根据编号获取用户信息")
    @GetMapping("/openId")
    public Message<ClassUser> getByOpenId(String openId){
        return classUserService.getUserInfoByOpenId(openId);
    }

    @ApiOperation(value = "根据编号更新用户信息")
    @PutMapping("/openId")
    public Message<?> updateClassUser(ClassUser c){
        return classUserService.updateUserInfoByUserId(c);
    }


    @ApiOperation(value = "用户注册新的")
    @PostMapping("/registernew")
    public Message<?> registernew(ClassUser c){
        c.setRegistrationTime(new Date());
        c.setUserStatus(0);
        c.setStuEdu(-1);
        return classUserService.register(c);
    }

    @ApiOperation(value = "账号密码登录新")
    @PostMapping("/login/new")
    public Message<?> zhanghaoLogin(String id,String user_pwd){

        return classUserService.zhanghaoLogin(id, user_pwd);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/")
    public Message<?> deleteById(String Id){
        return classUserService.deleteByPrimaryKey(Id);
    }

    @ApiOperation(value = "微信扫一扫")
    @PostMapping("/getWxConfig")
    public Message getWxConfig(String returnurl){
        String accessToken = accessTokenService.getAccessToken();
        String url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
        String result = sendGet(url,"utf-8",60000);
        System.out.println("Ticket:"+result);
        JSONObject json= JSONUtil.parseObj(result);
        String ticket = json.getStr("ticket");
        Map<String, String> sign = Sign.sign(ticket, returnurl);
        for (Map.Entry entry : sign.entrySet()) {
            System.out.println(entry.getKey() + "," + entry.getValue());
        }
        SaoYiSao saoYiSao = new SaoYiSao();
        saoYiSao.setAppId("wxff49e5a582930b5e");
        saoYiSao.setNonceStr(sign.get("nonceStr"));
        saoYiSao.setSignature(sign.get("signature"));
        saoYiSao.setTimestamp(sign.get("timestamp"));


        return Message.success(null).add(saoYiSao);
    }


    @ApiOperation(value = "修改用户状态")
    @PutMapping("/updateUserStatus")
    public Message updateUserStatusById(Integer updatedUserStatus,String id){
        return classUserService.updateUserStatusById(updatedUserStatus, id);
    }



    @ApiOperation(value = "条件查询")
    @PostMapping("/condition/search")
    public Message<List<ClassUser>> conditionsearch(String stuPhone,String stuIdCard,String stuUserName)
    {
        return classUserService.selectByStuPhoneAndStuIdCardAndStuUserName(stuPhone, stuIdCard, stuUserName);
    }

    @ApiOperation(value = "模糊搜索")
    @PostMapping("/mohu/search")
    public Message mohusearch(String str){

        return classUserService.mohusearch(str);
    }

    @ApiOperation(value = "多条件组合模糊查询")
    @PostMapping("/mohu/condition/search")
    public Message mohuConditionSearch(ClassUserSearch classUserSearch){
        return classUserService.mohuConditionSearch(classUserSearch);
    }
}
